@echo off
rem =========================================================
rem DesktopTimer packaging script
rem   Arg1 (optional): directory that contains DesktopTimer.exe
rem   Arg2 (optional): full path to windeployqt.exe
rem =========================================================
setlocal enableextensions enabledelayedexpansion

set "ROOT=%~dp0"
if "%ROOT:~-1%"=="\" set "ROOT=%ROOT:~0,-1%"

set "BIN_DIR=%ROOT%\bin"
set "DEFAULT_RELEASE=%ROOT%\build\Desktop_Qt_6_8_1_MinGW_64_bit-Release\app\release"
set "DEFAULT_DEBUG=%ROOT%\build\Desktop_Qt_6_8_1_MinGW_64_bit-Debug\app\debug"

set "APP_BUILD_DIR="
if not "%~1"=="" (
    set "APP_BUILD_DIR=%~1"
) else (
    if exist "%DEFAULT_RELEASE%\DesktopTimer.exe" (
        set "APP_BUILD_DIR=%DEFAULT_RELEASE%"
        echo [INFO] Using default Release build output.
    ) else (
        if exist "%DEFAULT_DEBUG%\DesktopTimer.exe" (
            set "APP_BUILD_DIR=%DEFAULT_DEBUG%"
            echo [INFO] Using default Debug build output.
        )
    )
)

if "%APP_BUILD_DIR%"=="" (
    echo [ERROR] DesktopTimer.exe not found in default locations.
    echo        Pass build directory containing DesktopTimer.exe as arg #1.
    exit /b 1
)

set "APP_EXE=%APP_BUILD_DIR%\DesktopTimer.exe"
if not exist "%APP_EXE%" (
    echo [ERROR] DesktopTimer.exe not found: %APP_EXE%
    exit /b 1
)

set "BUILD_CONFIG="
for %%I in ("%APP_BUILD_DIR%") do (
    set "BUILD_CONFIG=%%~nI"
)

set "KIT_ROOT="
call :ComputeKitRoot "%APP_BUILD_DIR%" KIT_ROOT
if "%KIT_ROOT%"=="" (
    echo [WARN] Unable to infer kit root; skipping module DLL copy.
) else (
    echo [INFO] Kit root: %KIT_ROOT%
)

set "WINDEPLOYQT=%QT_BIN%\windeployqt.exe"
if not "%~2"=="" (
    set "WINDEPLOYQT=%~2"
)
if not exist "%WINDEPLOYQT%" (
    set "WINDEPLOYQT=G:\\Qt\\6.8.1\\mingw_64\\bin\\windeployqt.exe"
)
rem 在 PATH 中尝试查找 windeployqt.exe（避免 usebackq 兼容性问题）
if not exist "%WINDEPLOYQT%" (
    for /f "delims=" %%W in ('where windeployqt.exe 2^>nul') do (
        if exist "%%~fW" (
            set "WINDEPLOYQT=%%~fW"
            goto :FOUND_WINDEPLOY
        )
    )
)
:FOUND_WINDEPLOY
if not exist "%WINDEPLOYQT%" (
    echo [ERROR] windeployqt.exe not found. Set QT_BIN or pass full path as arg #2.
    exit /b 1
)

echo [INFO] windeployqt: "%WINDEPLOYQT%"

echo [INFO] Preparing bin directory...
if exist "%BIN_DIR%" (
    rmdir /s /q "%BIN_DIR%"
)
mkdir "%BIN_DIR%"
mkdir "%BIN_DIR%\resource"
mkdir "%BIN_DIR%\translation"
mkdir "%BIN_DIR%\log"

echo [INFO] Copying DesktopTimer.exe ...
copy /y "%APP_EXE%" "%BIN_DIR%\DesktopTimer.exe" >nul
if errorlevel 1 (
    echo [ERROR] Failed to copy DesktopTimer.exe
    exit /b 1
)

echo [INFO] Copying static assets ...
if exist "%ROOT%\app\resource" xcopy /e /i /y "%ROOT%\app\resource" "%BIN_DIR%\resource" >nul
if exist "%ROOT%\fonts"        xcopy /e /i /y "%ROOT%\fonts"        "%BIN_DIR%\fonts" >nul
if exist "%ROOT%\gifs"         xcopy /e /i /y "%ROOT%\gifs"         "%BIN_DIR%\gifs" >nul
if exist "%ROOT%\docs"         xcopy /e /i /y "%ROOT%\docs"         "%BIN_DIR%\docs" >nul

echo [INFO] Copying translations ...
if exist "%ROOT%\app\translation" xcopy /e /i /y "%ROOT%\app\translation" "%BIN_DIR%\translation" >nul
if exist "%ROOT%\language_zh_cn.ts" xcopy /y "%ROOT%\language_*.ts" "%BIN_DIR%\translation" >nul

if not "%KIT_ROOT%"=="" if not "%BUILD_CONFIG%"=="" (
    call :CopyModuleDlls "%KIT_ROOT%" "%BUILD_CONFIG%"
)

echo [INFO] Running windeployqt ...
call "%WINDEPLOYQT%" --release --qm-dir "%BIN_DIR%\translation" --no-translations "%BIN_DIR%\DesktopTimer.exe"
set "WDQ_ERR=%ERRORLEVEL%"
if not "%WDQ_ERR%"=="0" (
    echo [ERROR] windeployqt failed. exitCode=%WDQ_ERR%
    exit /b %WDQ_ERR%
)

echo [INFO] Packaging completed. Output: %BIN_DIR%
exit /b 0

:CopyModuleDlls
setlocal
set "KIT=%~1"
set "CFG=%~2"
set "SUBMODULES=log core module service components view"
for %%S in (%SUBMODULES%) do (
    set "DLL_PATH=%KIT%\%%S\%CFG%\%%S.dll"
    if exist "!DLL_PATH!" (
        echo [INFO] Copying %%S.dll ...
        copy /y "!DLL_PATH!" "%BIN_DIR%" >nul
    )
)
endlocal
goto :eof

:ComputeKitRoot
setlocal
set "DIR=%~1"
if "%DIR%"=="" (
    endlocal & goto :eof
)
if "%DIR:~-1%"=="\" set "DIR=%DIR:~0,-1%"
for %%I in ("%DIR%\..") do set "DIR=%%~dpI"
for %%I in ("%DIR%\..") do set "DIR=%%~dpI"
if "%DIR%"=="" (
    endlocal & goto :eof
)
if "%DIR:~-1%"=="\" set "DIR=%DIR:~0,-1%"
endlocal & set "%~2=%DIR%"
goto :eof
